//
//  FSWMyController.m
//  voicetip_ios
//
//  Created by 伏圣文 on 2024/7/23.
//  Copyright © 2024 fusw. All rights reserved.
//

#import "FSWMyController.h"
#import <SDImageCache.h>
#import "XMGFileManager.h"

#define HMTCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

@interface FSWMyController ()
@property (weak, nonatomic) IBOutlet UILabel *accountL;
@property (weak, nonatomic) IBOutlet UILabel *cacheL;
@property (weak, nonatomic) IBOutlet UILabel *versionL;
@property (nonatomic, assign) NSInteger totalSize;
@end

@implementation FSWMyController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    self.accountL.text = @"xxx";
    self.cacheL.text = [self cacheStr];
    NSString *appVerion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    self.versionL.text =[NSString stringWithFormat:@"%@%@",@"v",appVerion];
   
}

- (IBAction)yinsiButton:(id)sender {
}
- (IBAction)aboutButton:(id)sender {
}

- (IBAction)clearButton:(id)sender {
    @weakify(self);
    [self alertTitle:@"清理缓存" andMessage:@"您确定要清理缓存吗？" andOK:@"确定" andCancle:@"取消" andBlock:^(UIAlertAction * _Nonnull action) {
        @strongify(self);
        [self cacheImageClear];
    }];
}



- (IBAction)zhuxiaoButton:(id)sender {
    @weakify(self);
    [self alertTitle:@"注销" andMessage:@"您确定要注销账号吗？" andOK:@"确定" andCancle:@"取消" andBlock:^(UIAlertAction * _Nonnull action) {
        @strongify(self);
        [self pop];
    }];
    
}
- (IBAction)logoutButton:(id)sender {
    @weakify(self);
    [self alertTitle:@"退出" andMessage:@"您确定要退出账号吗？" andOK:@"确定" andCancle:@"取消" andBlock:^(UIAlertAction * _Nonnull action) {
        @strongify(self);
        [self pop];
    }];
}


-(void)cacheImageClear{
    // 计算文件夹尺寸
    @weakify(self);
    [XMGFileManager getSizeOfDirectoryPath:HMTCachePath completion:^(int totalSize){
        @strongify(self);
//        self.totalSize = totalSize;
        NSString *cacheSizeStr = [self cacheStr];
       
        [XMGFileManager removeDirectory:HMTCachePath];
        [self clearTmpPics];
        [self clearAllWebViewData];
        [SVProgressHUD showWithStatus:[NSString stringWithFormat:@"%@",cacheSizeStr]];
        
        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC));
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
      
    }];
}

-(void)cacheWebviewClear{
    @weakify(self);
    [self alertTitle:@"提示" andMessage:@"确定要清除Cookies吗？" andOK:@"确定" andCancle:@"取消" andBlock:^(UIAlertAction * _Nonnull action) {
        @strongify(self);
        [self clearAllWebViewData];
        [SVProgressHUD showWithStatus:[NSString stringWithFormat:@"Cookies清除完成"]];
    }];
    
}

- (void)clearAllWebViewData {
    // Clear the webview cache...
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [self removeApplicationLibraryDirectoryWithDirectory:@"Caches"];
    [self removeApplicationLibraryDirectoryWithDirectory:@"WebKit"];
    // Empty the cookie jar...
    for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
    [self removeApplicationLibraryDirectoryWithDirectory:@"Cookies"];
}

- (void)removeApplicationLibraryDirectoryWithDirectory:(NSString *)dirName {
    NSString *dir = [[[[NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES) lastObject]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:dirName];
    if ([[NSFileManager defaultManager] fileExistsAtPath:dir]) {
        [[NSFileManager defaultManager] removeItemAtPath:dir error:nil];
    }
   
}

- (NSString *)cacheStr
{
    // 手机上 1000 点击1024
    // 1MB => 1000KB
    // 1KB => 1000B
    // 获取SDWebimage缓存尺寸
    float tmpSize = [[SDImageCache sharedImageCache] getSize];
    // 计算Cache缓存尺寸
    CGFloat cacheSizeF;
    NSString *sizeStr = @"0 byte";
    float size = tmpSize +_totalSize;
    if (size > 1000 * 1000) { // MB
        cacheSizeF = size / (1000.0 * 1000.0);
        sizeStr = [NSString stringWithFormat:@"%@(%.1fMB)",sizeStr,cacheSizeF];
    } else if (size > 1000) { // KB
        cacheSizeF = size / 1000.0;
        sizeStr = [NSString stringWithFormat:@"%@(%.1fKB)",sizeStr,cacheSizeF];
    } else if (size > 0) { // B
        sizeStr = [NSString stringWithFormat:@"%@(%lfB)",sizeStr,size];
    }
    
    sizeStr = [sizeStr stringByReplacingOccurrencesOfString:@".0" withString:@""];
    
    return sizeStr;
    
}
//清除SDWebImage图片缓存
- (void)clearTmpPics
{
     [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
       [[SDImageCache sharedImageCache] clearMemory];//可有可无
  
}


@end
